Skip to main content

processNotification

processNotification

  1. Start

    • Receive appointmentId and isInternal from NotificationDto.
  2. Fetch Appointment

    • Determine the repository to use:

      • If isInternal → use appointmentRepo.
      • Else → use appointmentEhrRepo.
    • Fetch the completed appointment using:

      • appointmentId
      • bookingIdField based on internal/external flag.
    • Log fetched appointment.

  3. Check if Appointment Exists

    • If no appointment found:

      • Log error.
      • Return { success: true }.
  4. Check for Existing Rating

    • Look for rating in ratingAppointmentRepo using bookingId.

    • If found:

      • Return { success: true, message: 'This appointment has already been rated' }.
  5. Prepare Customer Contact Details

    • Normalize the first phone number (US format).

    • Retrieve customer's email.

    • Fetch customer's opt-out preferences:

      • optedOutEmail
      • optedOutSMS
  6. Determine Notification Types to Send

    • Initialize typesToCheck:

      • If !optedOutEmail → add EMAIL.
      • If !optedOutSMS → add SMS.
    • If typesToCheck is not empty:

      • Call fetchExistingRatingNotifications() to get existing sent types.
  7. Build Notification Tasks

    • Initialize ratingNotificationsToTrack = [].

    • If Email should be sent:

      • Check:

        • !optedOutEmail
        • customer.email exists
        • EMAIL not in existingNotifications
      • Then:

        • Generate rating link.
        • Call sendEmailNotification().
        • Add new RatingNotification to ratingNotificationsToTrack.
    • If SMS should be sent:

      • Check:

        • !optedOutSMS
        • customerPhoneNumber exists
        • SMS not in existingNotifications
      • Then:

        • Generate rating link.
        • Call sendSmsNotification().
        • Add new RatingNotification to ratingNotificationsToTrack.
  8. Save Notification Records (If Any)

    • If ratingNotificationsToTrack.length > 0:

      • Increment Prometheus counter ratingsTypeProcessedCounter with label 'rated'.
      • Save records using ratingNotificationRepo.save().
  9. End

    • Return { success: true }.